<template>
	<view class="">

		<view class="content">
			<view>
				<view class="login-item">
					<image class="logo" src="@/static/basicprofile.jpeg"></image>
				</view>
				<view class="login-tip">容乾</view>

			</view>
			<view class="u-flex u-row-between">
				<!-- #ifdef MP-WEIXIN -->

				<u-button shape="circle" type="primary" hover-class="none" open-type="getPhoneNumber"
					@getphonenumber="handleGetPhoneNumber" class="u-flex-6 loginBtn"
					:custom-style="{ width: '95%', color: '#fff',backgroundColor:'#05C160',borderColor: '#05C160' }">
					微信一键登录
				</u-button>


				<!-- #endif -->


			</view>

			<view class="footer">
				<u-checkbox-group style="padding-top: 3px;">
					<u-checkbox size="14" v-model="checked" shape="circle" active-color="#05C160">1</u-checkbox>
				</u-checkbox-group>
				<view class="footer-tip">
					若手机号未注册将进入注册流程，注册即为同
					<span>用户服务协议</span>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import db from '@/common/localstorage.js'
	export default {
		data() {
			return {
				userId: '',
				checked: '',
			};
		},
		async onLoad() {
			let code = await this.getCode();
			console.log(11111)
			console.log(code)

			await this.toWxLogin(code)
		},
		methods: {
			handleAgree() {
				this.$u.toast('请阅读并同意用户服务协议及隐私政策');
			},

			
			handleGetPhoneNumber: function(e) {
				let that = this
				if (e.detail.errMsg === "getPhoneNumber:ok") {
					// 用户点击了允许授权  
					const encryptedData = e.detail.encryptedData;
					const iv = e.detail.iv;
					const code = e.detail.code;
					// 发送到后端服务器进行解密  
					uni.request({
						url: that.baseUrl + '/api/getPhone', // 替换为你的服务器接口  
						method: 'POST',
						data: {
							code: code,
							encryptedData: encryptedData,
							iv: iv
						},
						success: async function(res) {
							if (res.data.code==200) {
								let code = await that.getCode();
								let param = {
									phone:res.data.msg,
									code:code
								}
								 that.toRegister(param)
							} else {
								// 解密失败或服务器错误  
								wx.showToast({
									title: '登录失败',
									icon: 'none'
								});
							}
						},
						fail: function() {
							uni.showToast({
								title: '请求失败',
								icon: 'none'
							});
						}
					});
				} else if (e.detail.errMsg === "getPhoneNumber:fail user deny") {
					// 用户点击了拒绝  
					uni.showToast({
						title: '您拒绝了授权',
						icon: 'none'
					});
				}
			},

			getCode: async function() {
				return new Promise((resolve, reject) => {
					uni.login({
						success: function(res) {
							if (res.code) {
								resolve(res.code);
							} else {
								//login成功，但是没有取到code
								reject('未取得code');
							}
						},
						fail: function(res) {
							reject('用户授权失败wx.login');
						}
					});
				});
			},

			toWxLogin: async function(code) {
				let that = this
				await uni.request({
					url: that.baseUrl + '/api/wxLogin',
					// header: {
					// 	'Content-Type': 'application/x-www-form-urlencoded',
					// },
					method: 'POST',
					data: {
						code: code
					},
					success:async function(res) {
						if (res.data.code == 200) {
							that.$modal.msgInfo("登录成功")
							db.save('ACCESS_TOKEN', res.data.data.appUser.token)
							db.save('appToken', res.data.data.appUser.token)
							await db.save('userInfo', res.data.data.appUser)
							await db.save('permissions', res.data.data.appUser.permissions)
							await db.save('roles', res.data.data.appUser.roles)
							uni.reLaunch({
								url: '/pages/index/index'
							})
							// that.getUserInfo()
						} else {
							that.$modal.msgInfo(res.data.msg)
						}
						
					},
					fail: function(err) {
						console.log(err)
						// that.msgInfo('网络错误')
						that.$u.toast(err);
					}
				})
			},

			


			//登陆
			toRegister(data) {
				let that = this
				uni.request({
					url: that.baseUrl + '/api/wxRegister',
					// header: {
					// 	'Content-Type': 'application/x-www-form-urlencoded',
					// },
					method: 'POST',
					data: data,
					success:async  function(res) {
						if (res.data.code == 200) {
							that.$modal.msgInfo("登录成功")
							db.save('ACCESS_TOKEN', res.data.data.appUser.token)
							db.save('appToken', res.data.data.appUser.token)
							await db.save('userInfo', res.data.data.appUser)
							await db.save('permissions', res.data.data.appUser.permissions)
							await db.save('roles', res.data.data.appUser.roles)
							uni.reLaunch({
								url: '/pages/index/index'
							})
							// that.getUserInfo()
						} else {
							that.$modal.msgInfo(res.data.msg)
						}
						
					},
					fail: function(err) {
						// that.msgInfo('网络错误')
						that.$u.toast(err);
					}
				})
			},
			getUserInfo() {
				uni.showLoading({
					title: '加载中'
				});
				this.$get('/api/getInfo', {}).then(async res => {
					uni.hideLoading()
					if (res.data.code == 200) {
						await db.save('userInfo', res.data.user)
						await db.save('permissions', res.data.permissions)
						await db.save('roles', res.data.roles)
						uni.reLaunch({
							url: '/pages/index/index'
						})
					} else {
						this.$modal.msgInfo(res.data.msg)
					}
			
				})
			},

		}
	};
</script>

<style lang="scss" scoped>
	.footer {
		position: absolute;
		width: calc(100% - 80upx);
		bottom: 90upx;
		left: 40upx;
		display: flex;
		justify-content: center;

		.footer-tip {
			text-align: center;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 26rpx;
			color: #999999;
			// margin-left: 10upx;

			span {
				color: #409EFF;
			}
		}

		::v-deep .u-checkbox {
			display: block;
		}
	}


	.content {
		background-color: #fff;
		height: 100vh;
		padding: 100rpx 60rpx 0;
	}

	.login-item {
		display: flex;
		justify-content: center;
		padding-bottom: 40rpx;
		// border-bottom: 1rpx solid #dddddd;
	}

	.logo {
		display: block;
		width: 160rpx;
		height: 160rpx;
		border-radius: 50%;
		overflow: hidden;
		// border: 1px solid #999;
		// box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
	}

	.login-tip {
		font-family: SourceHanSansSC, SourceHanSansSC;
		font-weight: bold;
		font-size: 32rpx;
		color: #111111;
		text-align: center;
		margin-top: 20upx;
		margin-bottom: 50upx;

	}


	::v-deep .u-btn--primary {
		background-color: #05C160;
		border-color: #05C160;
	}
</style>